// pages/orderList/orderList.js
let app=getApp();
function Order() {
     var id;
     var image;
     var roomType;
     var startDate;
     var endDate;
     var name;
     var service;
     var originalPrice;
     var finalPrice;
     var roomCount;
     var customerList;
     var remark;
}
var id;
var allOrder;
Page({

     /**
      * 页面的初始数据
      */
     data: {
          stateList: [
               "全部", "待入住", "已入住", "已完成"
          ],
          common_row: {
               caption_class: 'caption',
               value_class: 'value'
          },

          OrderArray:[],
           
          selectedIndex: 0,

          //数据列表数组
          orderAll: undefined,
          orderTodo: undefined,
          orderDone: undefined,
          orderCancel: undefined,

          //是否可以上拉加载
          allLoadEnable: false,
          todoLoadEnable: false,
          doneLoadEnable: false,
          cancelLoadEnable: false,

          //数据为空或请求失败的提示语
          allEmpty: '全部订单为空',
          todoEmpty: '未入住订单为空',
          doneEmpty: '已入住订单为空',
          cancelEmpty: '已完成订单为空',
     },

     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {
          var that=this;
          console.log(options);
          id=parseInt(app.globalData.account.id);
          var pageType = options.type;
          if (pageType == 'all') {
               allOrder=[];
               //获取全部订单数据
               wx.request({
                    url:"http://101.37.14.167:8001/BookOrder/findBookOrderByAccount_Id",
                    method:"POST",
                    header:{'content-type':'application/json'},
                    data:{
                         'id':id,
                    },
                    success: function(res){
                         console.log(res)
                         var list=res.data.data;
                         console.log(list)
                         if(list.length!=0){
                              for(var i=0;i<list.length;i++){
                                   var order = new Order();
                                   order.id=list[i].id;
                                   order.image=list[i].roomList[0].photo;
                                   order.roomType=list[i].roomList[0].sn;
                                   order.startDate=list[i].arriveDate;
                                   order.endDate=list[i].leaveDate;
                                   order.originalPrice=list[i].originalPrice;
                                   order.finalPrice=list[i].finalPrice;
                                   order.name=list[i].roomList[0].roomType.name;
                                   order.roomCount=list[i].roomCount;
                                   order.customerList=list[i].customerList;
                                   order.remark=list[i].remark;
                                   allOrder.push(order);
                              }
                         }else{
                              wx.showToast({
                                   title:'全部订单为空',
                                   icon:'none',
                                   duration:2000
                              });
                         }
                         that.setData({
                              OrderArray:allOrder
                         });
                    },
                    fail:function(res){
                         console.log("失败"+res.data);
                         wx.showToast({
                              title:'请求失败',
                              icon:'none',
                              duration:2000
                         });
                    }
               })
               this.setData({
                    selectedIndex: 0,
               });
          } else if (pageType == 'todo') {
               allOrder=[];
               //获取待入住订单数据
               wx.request({
                    url:"http://101.37.14.167:8001/BookOrder/findBookOrderByAccount_WeChatOpenidAndStatus",
                    method:"POST",
                    header:{'content-type':'application/json'},
                    data:{
                         'openid':app.globalData.openId,
                         'status':0
                    },
                    success: function(res){
                         var list=res.data.data;
                         if(list.length!=0){
                              for(var i=0;i<list.length;i++){
                                   var order = new Order();
                                   order.id=list[i].id;
                                   order.image=list[i].roomList[0].photo;
                                   order.roomType=list[i].roomList[0].sn;
                                   order.startDate=list[i].arriveDate;
                                   order.endDate=list[i].leaveDate;
                                   order.originalPrice=list[i].originalPrice;
                                   order.finalPrice=list[i].finalPrice;
                                   order.name=list[i].roomList[0].roomType.name;
                                   order.roomCount=list[i].roomCount;
                                   order.customerList=list[i].customerList;
                                   order.remark=list[i].remark;
                                   allOrder.push(order);
                              }
                         }else{
                              wx.showToast({
                                   title:'待入住订单为空',
                                   icon:'none',
                                   duration:2000
                              });
                         }
                         that.setData({
                              OrderArray:allOrder
                         });
                    },
                    fail:function(res){
                         console.log("失败"+res.data);
                         wx.showToast({
                              title:'请求失败',
                              icon:'none',
                              duration:2000
                         });
                    }
               })
               this.setData({
                    selectedIndex: 1,
               });
          }

          var that = this;
          wx: setTimeout(function () {
               that.setData({
                    orderAll: [],
                    orderTodo: [],
                    orderDone: [],
                    orderCancel: [],
               });
          }, 2000);
     },
     bookRoom: function (e) {
          var index = e.currentTarget.dataset.index;
          var order = this.data.OrderArray[index];
          var customerList=encodeURIComponent(JSON.stringify(order.customerList));
          if(order.remark==null)order.remark='无';
          wx.navigateTo({
               url: '../orderDetail/orderDetail?finalPrice='+order.finalPrice +'&originalPrice='+order.originalPrice+ '&name=' + order.name + '&roomCount=' + order.roomCount + '&startDate=' + order.startDate + '&endDate=' + order.endDate +'&customerList='+customerList+'&roomType='+order.roomType+'&remark='+order.remark,
          })
     },
     swiperChange: function (e) {
          var detailIndex = e.detail.current;
          var source = e.detail.source;
          if (this.selectedIndex != detailIndex && source == 'touch') {
               this.setData({
                    selectedIndex: detailIndex
               });
          }
     },

     turnPage: function (e) {
          var that = this;
          var dataIndex = e.currentTarget.dataset.index;
          if (this.data.selectedIndex != dataIndex) {
               this.setData({
                    selectedIndex: dataIndex
               });
          } else {
               if (this.data.selectedIndex == 0) {
                    allOrder=[];
                    //获取全部订单数据
                    wx.request({
                         url:"http://101.37.14.167:8001/BookOrder/findBookOrderByAccount_Id",
                         method:"POST",
                         header:{'content-type':'application/json'},
                         data:{
                              'id':id,
                         },
                         success: function(res){
                              var list=res.data.data;
                              if(list.length!=0){
                                   for(var i=0;i<list.length;i++){
                                        var order = new Order();
                                        order.id=list[i].id;
                                        order.image=list[i].roomList[0].photo;
                                        order.roomType=list[i].roomList[0].sn;
                                        order.startDate=list[i].arriveDate;
                                        order.endDate=list[i].leaveDate;
                                        order.originalPrice=list[i].originalPrice;
                                        order.finalPrice=list[i].finalPrice;
                                        order.name=list[i].roomList[0].roomType.name;
                                        order.roomCount=list[i].roomCount;
                                        order.customerList=list[i].customerList;
                                        order.remark=list[i].remark;
                                        allOrder.push(order);
                                   }
                              }else{
                                   wx.showToast({
                                        title:'全部订单为空',
                                        icon:'none',
                                        duration:2000
                                   });
                              }
                              that.setData({
                                   OrderArray:allOrder
                              });
                         },
                         fail:function(res){
                              console.log("失败"+res.data);
                              wx.showToast({
                                   title:'请求失败',
                                   icon:'none',
                                   duration:2000
                              });
                         }
                    })
               } else if (this.data.selectedIndex == 1) {
                    allOrder=[];
                    //获取待入住订单数据
                    wx.request({
                         url:"http://101.37.14.167:8001/BookOrder/findBookOrderByAccount_WeChatOpenidAndStatus",
                         method:"POST",
                         header:{'content-type':'application/json'},
                         data:{
                              'openid':app.globalData.openId,
                              'status':0
                         },
                         success: function(res){
                              var list=res.data.data;
                              if(list.length!=0){
                                   for(var i=0;i<list.length;i++){
                                        var order = new Order();
                                        order.id=list[i].id;
                                        order.image=list[i].roomList[0].photo;
                                        order.roomType=list[i].roomList[0].sn;
                                        order.startDate=list[i].arriveDate;
                                        order.endDate=list[i].leaveDate;
                                        order.originalPrice=list[i].originalPrice;
                                        order.finalPrice=list[i].finalPrice;
                                        order.name=list[i].roomList[0].roomType.name;
                                        order.roomCount=list[i].roomCount;
                                        order.customerList=list[i].customerList;
                                        order.remark=list[i].remark;
                                        allOrder.push(order);
                                   }
                              }else{
                                   wx.showToast({
                                        title:'待入住订单为空',
                                        icon:'none',
                                        duration:2000
                                   });
                              }
                              that.setData({
                                   OrderArray:allOrder
                              });
                         },
                         fail:function(res){
                              console.log("失败"+res.data);
                              wx.showToast({
                                   title:'请求失败',
                                   icon:'none',
                                   duration:2000
                              });
                         }
                    })
               } else if (this.data.selectedIndex == 2) {
                    allOrder=[];
                    //获取已入住订单数据
                    wx.request({
                         url:"http://101.37.14.167:8001/BookOrder/findBookOrderByAccount_WeChatOpenidAndStatus",
                         method:"POST",
                         header:{'content-type':'application/json'},
                         data:{
                              'openid':app.globalData.openId,
                              'status':1
                         },
                         success: function(res){
                              var list=res.data.data;
                              if(list.length!=0){
                                   for(var i=0;i<list.length;i++){
                                        var order = new Order();
                                        order.id=list[i].id;
                                        order.image=list[i].roomList[0].photo;
                                        order.roomType=list[i].roomList[0].sn;
                                        order.startDate=list[i].arriveDate;
                                        order.endDate=list[i].leaveDate;
                                        order.originalPrice=list[i].originalPrice;
                                        order.finalPrice=list[i].finalPrice;
                                        order.name=list[i].roomList[0].roomType.name;
                                        order.roomCount=list[i].roomCount;
                                        order.customerList=list[i].customerList;
                                        order.remark=list[i].remark;
                                        allOrder.push(order);
                                   }
                              }else{
                                   wx.showToast({
                                        title:'已入住订单为空',
                                        icon:'none',
                                        duration:2000
                                   });
                              }
                              that.setData({
                                   OrderArray:allOrder
                              });
                         },
                         fail:function(res){
                              console.log("失败"+res.data);
                              wx.showToast({
                                   title:'请求失败',
                                   icon:'none',
                                   duration:2000
                              });
                         }
                    })
               } else if (this.data.selectedIndex == 3) {
                    allOrder=[];
                    //获取已完成订单数据
                    wx.request({
                         url:"http://101.37.14.167:8001/BookOrder/findBookOrderByAccount_WeChatOpenidAndStatus",
                         method:"POST",
                         header:{'content-type':'application/json'},
                         data:{
                              'openid':app.globalData.openId,
                              'status':2
                         },
                         success: function(res){
                              var list=res.data.data;
                              if(list.length!=0){
                                   for(var i=0;i<list.length;i++){
                                        var order = new Order();
                                        order.id=list[i].id;
                                        order.image=list[i].roomList[0].photo;
                                        order.roomType=list[i].roomList[0].sn;
                                        order.startDate=list[i].arriveDate;
                                        order.endDate=list[i].leaveDate;
                                        order.originalPrice=list[i].originalPrice;
                                        order.finalPrice=list[i].finalPrice;
                                        order.name=list[i].roomList[0].roomType.name;
                                        order.roomCount=list[i].roomCount;
                                        order.customerList=list[i].customerList;
                                        order.remark=list[i].remark;
                                        allOrder.push(order);
                                   }
                              }else{
                                   wx.showToast({
                                        title:'已完成订单为空',
                                        icon:'none',
                                        duration:2000
                                   });
                              }
                              that.setData({
                                   OrderArray:allOrder
                              });
                         },
                         fail:function(res){
                              console.log("失败"+res.data);
                              wx.showToast({
                                   title:'请求失败',
                                   icon:'none',
                                   duration:2000
                              });
                         }
                    })
               }
          }
     },

     /**
      * 搜索订单
      */
     searchEvent: function (e) {

     },
     /**
      * 搜索框输入监听
      */
     inputListener: function (e) {

     },

     /**
      * 生命周期函数--监听页面初次渲染完成
      */
     onReady: function () {

     },

     /**
      * 生命周期函数--监听页面显示
      */
     onShow: function () {

     },

     /**
      * 生命周期函数--监听页面隐藏
      */
     onHide: function () {

     },

     /**
      * 生命周期函数--监听页面卸载
      */
     onUnload: function () {

     },

     /**
      * 页面相关事件处理函数--监听用户下拉动作
      */
     onPullDownRefresh: function () {

     },

     /**
      * 页面上拉触底事件的处理函数
      */
     onReachBottom: function () {

     },

     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function () {

     }
})